package com.javafactroy.kunlong.bip.netty;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.event.annotation.AfterTestClass;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.javafactroy.kunlong.bip.IcpAppl;
import com.javafactroy.kunlong.bip.icp.context.connpool.nettyclient.NettyClientPool;
import com.javafactroy.kunlong.bip.icp.message.base.iface.IMessage;

import com.javafactroy.kunlong.bip.icp.message.dxp.base.DxpMsg;
import com.javafactroy.kunlong.bip.icp.netty.client.framework.NettyClient;
import java.io.IOException;
import java.util.function.Function;

@SpringBootTest(classes = {IcpAppl.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@Slf4j
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
//@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class TestNettyClient {


    @Autowired
    NettyClientPool clientPool;
    NettyClient nettyClient;

    DxpMsg req;

    @BeforeAll
    public void setUpSuite() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    @AfterTestClass
    public void tearDownSuite() {

    }

    @BeforeEach
    public void setUp() throws Exception {
        req = DxpMsg.buildMessage();
        clientPool.setIpPort("127.0.0.1", 6666);
        nettyClient = clientPool.borrowObject();

   }

    @AfterEach
    public void tearDown() {
        clientPool.returnObject(nettyClient);
    }

    @Test
    public void test001_send2NettyClient() throws IOException {

        //IMessage resp = nettyClient.sendSyncMsg(req);
        //log.info("test001_send2NettyClient resp= {}", JSON.toJSONString(resp));
        Function<String, String> f = a -> a;
        log.info("{}", f.apply("111"));

    }

    @Test
    public void test002_send2iNettyTcpClient() {
        DxpMsg resp = (DxpMsg) nettyClient.sendSyncMsg(req);
        log.info("test002_send2iNettyTcpClient resp={}", resp.toJSONString());
    }

    @Test
    public void test003_send2NettyClientService() {
        DxpMsg resp = (DxpMsg) nettyClient.sendSyncMsg(req);
        log.info("test003_send2NettyClientService resp={}", resp.toJSONString());
    }

    @Test
    public void test004_send2SocketNetty() throws IOException {
        IMessage resp = nettyClient.sendSyncMsg(req);
        log.info("test004_send2SocketNetty resp= {}", resp);

    }

}
